package com.spn.blog.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spn.blog.common.dao.pojo.LoginUser;
import com.spn.blog.common.dao.pojo.SysUser;
import com.spn.blog.common.dao.pojo.SysUserTpusers;
import com.spn.blog.login.mapper.SysUserTpusersMapper;
import com.spn.blog.login.service.SysUserTpusersService;
import com.spn.blog.login.privider.AccessPrivider;
import com.spn.blog.common.vo.ErrorCode;
import com.spn.blog.common.vo.Result;
import com.spn.blog.common.vo.params.LoginParam;
import com.spn.blog.common.vo.params.TpuserBindParam;
import com.spn.blog.service.login.LoginService;
import com.spn.blog.service.login.SysUserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class SysUserTpusersServiceImpl implements SysUserTpusersService {
    private static final String salt = "spn!@#";
    @Autowired
    private SysUserTpusersMapper sysUserTpusersMapper;
    @Autowired
    private AccessPrivider accessPrivider;


    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Result userbind(TpuserBindParam tpuserBindParam) {
        String email = tpuserBindParam.getEmail();
        String rawPassword = tpuserBindParam.getPassword();
        String token = tpuserBindParam.getToken();

        // 能进入此方法说明还没有绑定，就去拿password  email 去和数据库做对比
        SysUser sysUser = sysUserService.findUserByEmail(email);

        // 密码匹配
        boolean matches = passwordEncoder.matches(rawPassword + salt, sysUser.getPassword());
        if (!matches) {
            return Result.fail(ErrorCode.ACCOUNT_NOT_EXIST.getCode(), ErrorCode.ACCOUNT_NOT_EXIST.getMsg());
        }

        // 邮箱匹配
        if (!sysUser.getEmail().equals(email)) {
            return Result.fail(ErrorCode.ACCOUNT_NOT_EXIST.getCode(), ErrorCode.ACCOUNT_NOT_EXIST.getMsg());
        }

        // 到这说明用户存在，可以进行绑定
        // 1. 根据token获取tp用户的id
        SysUser tpUser = accessPrivider.getSysUser(token);
        SysUserTpusers sysUserTpusers = new SysUserTpusers();
        sysUserTpusers.setSysUserId(String.valueOf(sysUser.getId()));
        sysUserTpusers.setTpusersId(String.valueOf(tpUser.getId()));
        sysUserTpusersMapper.insert(sysUserTpusers);

        LoginParam loginParam = new LoginParam();
        loginParam.setAccount(sysUser.getAccount());
        loginParam.setPassword(rawPassword);
        return loginService.login(loginParam);
    }

    @Override
    public Result secondlogin(TpuserBindParam tpuserBindParam) {
        String token = tpuserBindParam.getToken();
        if (token==null) return Result.fail(ErrorCode.TOKEN_NULL_ERROR.getCode(), ErrorCode.TOKEN_NULL_ERROR.getMsg());
        SysUser tpUser = accessPrivider.getSysUser(token);

        LambdaQueryWrapper<SysUserTpusers> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserTpusers::getTpusersId, tpUser.getId());
        SysUserTpusers sysUserTpusers = sysUserTpusersMapper.selectOne(queryWrapper);
        String sysUserId = sysUserTpusers.getSysUserId();
        SysUser currentUser = sysUserService.findUserById(sysUserId);
        if (currentUser == null) {
            //说明没有这个用户，
            return Result.fail(ErrorCode.ACCOUNT_NOT_EXIST.getCode(), ErrorCode.ACCOUNT_NOT_EXIST.getMsg());
        }
        LoginParam loginParam = new LoginParam();
        loginParam.setAccount(currentUser.getAccount());
        loginParam.setPassword(currentUser.getPassword());
        return loginService.login(loginParam);
    }
}
